package net.ojbk.timeSpace.test.web.handllers.impl;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import net.ojbk.timeSpace.test.beans.APIResult;
import net.ojbk.timeSpace.test.beans.Img;
import net.ojbk.timeSpace.test.beans.TimeAxis;
import net.ojbk.timeSpace.test.beans.TimeBlade;
import net.ojbk.timeSpace.test.beans.User;
import net.ojbk.timeSpace.test.common.api.APICodeConstant;
import net.ojbk.timeSpace.test.common.sgz.LocalAddress;
import net.ojbk.timeSpace.test.service.IAxisService;
import net.ojbk.timeSpace.test.service.IImgService;
import net.ojbk.timeSpace.test.util.UUIDTool;
import net.ojbk.timeSpace.test.web.handllers.IAxisController;

@Controller
public class AxisController implements IAxisController {

	@Autowired
	@Qualifier("axisService")
	IAxisService axisService;
	@Autowired
	@Qualifier("imgService")
	IImgService imgService;

	@SuppressWarnings("unchecked")
	@Override
	@RequestMapping(value = "pushTime.do")
	public ModelAndView doPushBlade(int axisId, String title, String content, MultipartFile imgs,
			HttpSession session) throws Exception{
		// 未做权限控制
		
		ServletContext sc = session.getServletContext();
		Map<Integer, TimeAxis> timeAxisMap = (Map<Integer, TimeAxis>) sc.getAttribute("timeAxisMap");
		// 通过servletcontext获取
		TimeAxis timeAxis = timeAxisMap.get(axisId);
		
		// 构造,回向该对象写入 timeBladeId 
		TimeBlade newBlade = new TimeBlade();
		newBlade.setMsgConfig(true);
		newBlade.setTimeBladeInfo(content);
		newBlade.setTimeBladeTitel(title);
		axisService.addTimeBlade(timeAxis, newBlade);
		
		// 这样，使用图片UUid标识成功实现 图片存储逻辑 与 server分离
		if (!imgs.isEmpty()) {
			String path = LocalAddress.imgAddress;
			
			String oriFileName = imgs.getOriginalFilename();
			if (oriFileName.endsWith(".jpg")) {
				Img imgR = new Img();
				imgR.setImgUUID(UUIDTool.getNewUUID());
				imgR.setTimeBladeId(newBlade.getTimeBladeId());
				
				// 存入文件
				File file = new File(path, imgR.getImgUUID() + ".jpg");
				imgs.transferTo(file);
				// 写入数据库
				imgService.addImg(imgR);
				// 更改img集合
				/*Map<String, Img> imgMap = (Map<String, Img>) sc.getAttribute("imgMap");
				imgMap.put(imgR.getImgUUID(), imgR);*/
				
			} 
		}
		return new ModelAndView("redirect:/timeBlade.html?timeBladeId=" + newBlade.getTimeBladeId());
	}

	@Override
	public ModelAndView doShowHome(String axisId, HttpSession session) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	@RequestMapping(value = "modifyMsgPw.do")
	@ResponseBody
	public Object doModifyMsgPw(String newPw, String axisId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (null == user)
			return APIResult.createFail(new APICodeConstant(), "Who are you?");
		TimeAxis axis = null;
		try {
			axis = axisService.getOneAxis(Integer.parseInt(axisId));
		} catch (Exception e) {
			return APIResult.createError(new APICodeConstant(), "you???");
		}

		if (axis == null)
			return APIResult.createError(new APICodeConstant(), "axisId error");

		if (!axis.getUid().equals(user.getUid()))
			return APIResult.createFail(new APICodeConstant(), "Not you.");

		axisService.modifyMsgPw(axis, newPw);

		return APIResult.createSuccessMessage(new APICodeConstant(), "Success");
	}

}
